<template>
  <el-drawer v-model="drawerProps.isView" :destroy-on-close="true" size="30%" :title="`${drawerProps.title}题库`">
    <el-form
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="!drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="题库名称" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写题库名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属章节" prop="chapter_id">
        <TreeFilter
          label="name"
          title="章节列表(单选)"
          :request-api="getChapterMenuApi"
          @change="changeTreeFilter"
          :api-params="drawerProps.course_id.toString()"
          :default-value="selected_chapter_id"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleSubmit"> 提交 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="editBankInfoDrawer">
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import TreeFilter from "@/components/TreeFilter/index.vue";
import { getChapterMenuApi } from "@/api/modules/course";
import { Bank } from "@/api/interface";

const rules = reactive({
  name: [{ required: true, message: "请填写题库名称" }],
  chapter_id: [{ required: true, message: "请选择所属章节" }]
});
const selected_chapter_id = ref<number>(0);
interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Bank.BankInfo>;
  api?: (params: any) => Promise<any>;
  getTableList?: (param: any) => void;
  getTableListParam?: any;
  chapter_id?: number;
  // chapterOptions: [];
  type: string;
  course_id: number;
}
const drawerProps = ref<DrawerProps>({
  course_id: 0,
  type: "",
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: any) => {
  drawerProps.value = params;
  selected_chapter_id.value = params.chapter_id;
};
// 提交数据（新增/编辑）
const handleSubmit = async () => {
  if (drawerProps.value.row.name === "") {
    ElMessage.error({ message: `请填写题库名称！` });
    return;
  }
  try {
    const bankInfo = reactive<Bank.BankInfo>({
      bank_id: drawerProps.value.row.bank_id?.toString() || "",
      name: drawerProps.value.row.name || "",
      provider: drawerProps.value.row.provider || "",
      chapter_id: selected_chapter_id.value?.toString() || "0",
      type: drawerProps.value.type,
      course_id: drawerProps.value.course_id.toString() || ""
    });
    const data = await drawerProps.value.api!({ ...bankInfo });
    if (data.code !== "") {
      ElMessage.error(data.msg);
      return;
    }
    ElMessage.success({ message: `提交成功！` });
    drawerProps.value.getTableList!(drawerProps.value.getTableListParam);
    drawerProps.value.isView = false;
  } catch (error) {
    console.log(error);
  }
  return true;
};
const changeTreeFilter = (val: string) => {
  ElMessage.success(`你选择了 id 为 ${val} 的数据🤔`);
  selected_chapter_id.value = Number(val);
};
defineExpose({
  acceptParams
});
</script>
<style scoped lang="scss">
@import "questionEditor";
</style>
